/**
* @author Ucar Sahin
*
* 09.06.2011
* ProtocolData.java
* 
*/

package at.fhv.easyDietWeb.shared.util;

import java.util.Date;
import java.util.List;

import com.google.gwt.user.client.rpc.IsSerializable;

import at.fhv.easyDietWeb.shared.util.wrappers.DayWrapper;

/**
 * a wrapper class for all the user input for the nutritional journal
 * @author Spiegel Philipp
 *
 */
public class ProtocolData implements IsSerializable
{
	private Date _from;
	private Date _until;
	private List<DayWrapper> _days;
	
	/**
	 * default constructor
	 */
	public ProtocolData(){}
	
	/**
	 * constructor
	 * @param from start date
	 * @param until end date
	 * @param days list of all days in the journal
	 */
	public ProtocolData(Date from, Date until, List<DayWrapper> days)
	{
		_from = from;
		_until = until;
		_days = days;
	}
	
	/**
	 * returns the start date
	 * @return start date
	 */
	public Date getFromDate()
	{
		return _from;
	}
	
	/**
	 * returns the end date
	 * @return end date
	 */
	public Date getUntilDate()
	{
		return _until;
	}
	
	/**
	 * returns the list of the days
	 * @return list of the days
	 */
	public List<DayWrapper> getDays()
	{
		return _days;
	}
	
	/**
	 * adds a day to the wrapper
	 * @param day the day
	 */
	public void addDay(DayWrapper day)
	{
		_days.add(day);
	}
	
	/**
	 * checks, if the day is already in the wrapper
	 * @param day the day to search for
	 * @return true, if the day is existing
	 */
	public boolean containsDay(DayWrapper day)
	{
		for (DayWrapper d : _days)
		{
			if (d.getDay().equals(day.getDay()))
			{
				return true;
			}
		}
		return false;
	}
	
	/**
	 * override the corresponding day with the new information
	 * @param day
	 */
	public void overrideDay(DayWrapper day)
	{
		int index = -1;
		for (DayWrapper d : _days)
		{
			if (day.getDay().equals(d.getDay()))
			{
				index = _days.indexOf(d);
			}
		}
		if (index > -1)
		{
			_days.remove(index);
			_days.add(day);
		}
	}
}
